﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Space_Cleaner.Utils;

namespace Space_Cleaner.SpaceCleaner
{
    class SC_Rocket : SC_SpaceObject
    {
        private bool isStarted = false;
        private float orbitHeight;
        private SC_Satellite mySatelite;

        public Boolean IsStarted
        {
            get { return this.isStarted; }
        }

        public float OrbitHeight
        {
            get { return this.orbitHeight; }
        }

        public SC_Satellite MySatelite
        {
            get { return mySatelite; }
        }

        public SC_Rocket(Texture2D _texture)
            : base(new Vector2(0.1f, 0), 0, 450.0f, _texture)
        {
            size = 0.08f;
        }

        public SC_Rocket()
            : base()
        {
        }

        public void init(float startAngle, float orbitHeight, SC_Satellite satellite)
        {
            this.rotation = MathHelper.ToRadians(90.0f);
            rotationVel = 0.0f;
            isStarted = false;
            activated = true;
            position = new Vector2(0.1f, 0);
            velocity = Vector2.Zero;
            acceleration = Vector2.Zero;

            this.orbitHeight = orbitHeight;
            mySatelite = satellite;
        }

        public void start()
        {
            isStarted = true;
            activated = true;
        }

        public override void update(TimeSpan elapsedTime)
        {
            if (isStarted)
            {
                Vector2 direction = position;
                direction.Normalize();
                direction *= 0.2f;
                position += direction;

                rotation += rotationVel * elapsedTime.Milliseconds;

                position = Vector2.Transform(position, Matrix.CreateRotationZ(-0.0000001f * elapsedTime.Milliseconds * position.Length()));
                this.rotation += (-0.0000002f * elapsedTime.Milliseconds * position.Length());

                if (orbitHeight <= position.Length())
                {
                    if (mySatelite != null)
                    {
                        mySatelite.Velocity = Vector2.Zero;
                        mySatelite.Activated = true;
                        mySatelite.Position = position;
                        Vector2 Force = position = Vector2.Transform(position, Matrix.CreateRotationZ(MathHelper.ToRadians(90.0f)));
                        Force.Normalize();
                        //Force *= -100.0f;
                        Force *= -0.378f;
                        mySatelite.Velocity = Force;
                        //mySatelite.addForce(Force);
                    }
                    isStarted = false;
                    Activated = false;
                }
            }
        }

        public override void CopyValues(SC_SpaceObject _SpaceObject, float _Scale)
        {
            base.CopyValues(_SpaceObject, _Scale);

            if (_SpaceObject is SC_Rocket)
            {
                isStarted = ((SC_Rocket)_SpaceObject).IsStarted;
                orbitHeight = ((SC_Rocket)_SpaceObject).OrbitHeight;
                mySatelite = new SC_Satellite();
                mySatelite.CopyValues(((SC_Rocket)_SpaceObject).mySatelite, 1);
            }
        }

        public override void draw(SpriteBatch _spriteBatch, SC_Display _display)
        {
            if (this.IsStarted) base.draw(_spriteBatch, _display);
            drawOrbit(this, _spriteBatch, _display);
        }
    }
}
